<?php 
/**
 * 车辆管理类
 * @Author    紫梦
 * @DateTime  2017-11-03
 * @copyright 响响租车
 * @version   1.0
 * @UpdateTime 2017-11-10
 */
namespace app\wxapp\controller;
use app\common\model\Cars as CarsModel;
use app\common\request\ClientLai;
use app\common\controller\Gps;
use app\common\model\CarsInfo;
use think\Exception;


class Vehicle extends Base
{
	public function details($id=1){
		$cars = CarsModel::with('brand,agency')->find($id);
        if($cars['supplier_no'] == 1376920002){
            $config = config('gz_blt');
            $obj = new ClientLai($config['user_name'],$config['password']);
            $res = $obj->request_post(rtrim($config['url'],'/').'/api/vehicle/carData/getRtInfo',http_build_query(['car_unique_id'=>$cars['car_unique_id']]));
            $arr = json_decode($res,true);
            if($arr['result'] == 0){
                if(empty($arr['data'][0])){
                    $info = null;
                }else{
                    $cars_data = $arr['data'][0];
                    $cars_data['long_term_lease'] = $cars_data['supplier_rent_type'];
                    $info = $cars_data; 
                }
            }else{
                $info = null;
            }

        }elseif($cars['supplier_no'] == 1376920003){
            $config = config('local_iot');
            $obj = new ClientLai($config['user_name'],$config['password']);
            $res = $obj->request_post(rtrim($config['url'],'/').'/car/getRtInfo',http_build_query(['car_unique_id'=>$cars['car_unique_id']]));
            $arr = json_decode($res,true);
            if($arr['result'] == 0){
                if(empty($arr['data'][0])){
                    $info = null;  
                }else{
                    $info = $arr['data'][0];
                    
                }
            }else{
                $info = null;
            }

        }elseif($cars['supplier_no'] == 1376920001){
            $carInfo = CarsInfo::get($cars['car_unique_id']);
            if($carInfo){
                $info = $carInfo;
              
            }else {
                $info = null;
            }

        }else{
            $info = null;
        }

        if($info){
            $result = Gps::geocode($info['longitude'],$info['latitude']);
            if($result){
                $gaodeMap = json_decode($result,true);
                try{

                    $data['formatted_address'] = $gaodeMap['regeocode']['formatted_address']??'';
                    $data['street'] = $gaodeMap['regeocode']['addressComponent']['streetNumber']["street"]?$gaodeMap['regeocode']['addressComponent']['streetNumber']["street"]:'';
                    $data['number'] = $gaodeMap['regeocode']['addressComponent']['streetNumber']["number"]?$gaodeMap['regeocode']['addressComponent']['streetNumber']["number"]:'';

                }catch(\Exception $e){
                    $data['formatted_address'] = null;
                    $data['street'] = null;
                }

            }else{
                $data['formatted_address'] = null;
                $data['street'] = null;
            }  
        }else{
            $data['formatted_address'] = null;
            $data['street'] = null;
        }

        $data['cars'] = $cars;
        $data['info'] = $info;
        $data['holder_name'] = $cars->holder_name;
        return $data;
	}
}